<?php
namespace Modules\Activity\Entities\Jika;;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;

use Modules\Order\Traits\Skuable;

use Modules\Asset\Entities\PackageSku;

// 一番赏活动 副表
class Sku extends Model
{
    use Skuable;
    
    protected $table = 'jika_skus';

   	protected $guarded = [];
    static public $resourcename = '集卡奖品';

    protected $casts = [
        'assets' => 'json',
    ];

    // 所属活动
    public function activity()
    {
        return $this->belongsTo('\Modules\Activity\Entities\Jika\Activity', 'activity_id');
    }

    // 所属任务
    public function task()
    {
        return $this->belongsTo('\Modules\Activity\Entities\Jika\Task', 'task_id');
    }

    public function getType($type) 
    {
        $map = [
            'base_sku_type' => 'jika_sku',
            'node_type' => 'jika'
        ];

        return @$map[$type];
    }

    // 
    public function getNodeEntity()
    {
        return $this->activity;
    }

    // 获取列表
    static public function beforeGetList($options) 
    {
        if (@$options['task_id']) {
            $options['where']['task_id'] = $options['task_id'];
        }

    	return $options;
    }

    // 赠送成一个package sku
    public function sendAsPackageSku($user)
    {
        $order = NULL;
        $sku = $this;
        $node = $this->activity;
        $info = [
            'uuid' => uniqid(),
            'user_id' => $user->id,
            'title' => $sku->title,
            'number' => $sku->number,
            'thumb' => $sku->thumb,
            'sku_type' => 2,  // 自定义实物
            'product_id' => $sku->product_id,
            'sku_id' => $sku->id,
            'assets' => $sku->assets,
            'total' => 1,
            'is_special_sku' => 0,
            'score_price' => $sku->getSellingPrice('score'),
            'money_price' => $sku->getSellingPrice('money'),
            'pay_order_id' => @$order->id,
            'activity_id' => NULL,
            'activity_type' => NULL,
            'activity_uuid' => NULL,
            'node_title' => $node->title,
            'node_money_price' => $node->money_price,
            'node_score_price' => $node->score_price,
            'node_id' => $node->id,
            'node_type' => $node->getType('node_type'),
            'node_uuid' => $node->uuid,
            'open_indexs' => NULL,
            'open_index' => NULL,
            'options' => NULL,
            'source_type' => 4,     // 兑换码
            'presell_date'  => NULL,
            'room_id' => NULL,
            'sku_id' => $sku->id,
            'use_lucky_score' => 0,
            'is_return_saleable' => 0
        ];

        return PackageSku::create($info);
    }
}
